import { defineConfig } from "vite"
import vue from "@vitejs/plugin-vue"
import path from "path"
import { baseUrl } from "./public/static/config"
// npm install -D unplugin-vue-components unplugin-auto-import
// import AutoImport from "unplugin-auto-import/vite"
// import Components from "unplugin-vue-components/vite"
// import { ElementPlusResolver } from "unplugin-vue-components/resolvers"
const isProduction = process.env.NODE_ENV === "production"

// https://vitejs.dev/config/
export default defineConfig({
    base: "./", // 设置打包路径
    productionSourceMap: !isProduction, //关闭生产环境下的SourceMap映射文件
    // vue3版本3.4.x以上需要配置这个
    define: { 
        __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: "true",
    },
    plugins: [
        vue(),
        // AutoImport({
        //     resolvers: [ElementPlusResolver()],
        // }),
        // Components({
        //     resolvers: [ElementPlusResolver()],
        // }),
    ],
    resolve: {
        alias: {
            // 如果报错__dirname找不到，需要安装node,执行npm install @types/node --save-dev
            "@": path.resolve(__dirname, "src"),
            "@assets": path.resolve(__dirname, "src/assets"),
            "@store": path.resolve(__dirname, "src/store"),
            "@mixins": path.resolve(__dirname, "src/mixins"),
            "@components": path.resolve(__dirname, "src/components"),
            "@common": path.resolve(__dirname, "src/assets/common"),
            "@images": path.resolve(__dirname, "src/assets/images"),
            "@views": path.resolve(__dirname, "src/views")
        },
    },
    css: {
        sourceMap: !isProduction, // css sourceMap 配置
        preprocessorOptions: {
            less: {
                modifyVars: {
                    hack: `true; @import (reference) "${path.resolve("src/assets/css/base.less")}";`,
                },
                javascriptEnabled: true,
            },
        },
    },
    build: {
        outDir: "dist",
        assetsDir: "assets", //指定静态资源存放路径
        minify: "terser", // 混淆器，terser 构建后文件体积更小，'terser' | 'esbuild'
        sourcemap: false, // 是否构建source map 文件
        chunkSizeWarningLimit: 1500, // chunk 大小警告的限制，默认500KB
        // rollupOptions: {
        //     output: {
        //         // 最小化拆分包
        //         manualChunks(id) {
        //             if (id.includes('node_modules')) {
        //                 return id.toString().split('node_modules/')[1].split('/')[0].toString();
        //             }
        //         },
        //         chunkFileNames: 'js/[name].[hash].js', // 用于命名代码拆分时创建的共享块的输出命名，[name]表示文件名,[hash]表示该文件内容hash值
        //     }
        // },
        terserOptions: {
            compress: {
                drop_console: true, // 打包时删除console
                drop_debugger: true, //打包时删除 debugger
            },
            output: {
                comments: true, // 去掉注释内容
            },
        },
    },
    server: {
        https: false, // 是否开启 https
        open: false, // 是否自动在浏览器打开
        cors: true, // 允许跨域
        port: 3000, // 端口号
        host: "0.0.0.0",
        proxy: {
            "/v1": {
                target: `${baseUrl}/v1/`, // 后台接口
                changeOrigin: true,
                secure: true, // 如果是https接口，需要配置这个参数
                // ws: true, //websocket支持
                rewrite: (path) => path.replace(/^\/v1/, ""), // 匹配/supplier/admin  格式为：/^\/supplier\/admin/
            }
        },
    },
    // 引入第三方的配置
    optimizeDeps: {
        include: [],
    },
})
